/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.data.exchange.runner.master;

import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.proxy.DataProxyRequest;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.proxy.DataProxyResponse;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskFinishOrderRequest;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.stat.TaskInfo;
import cn.ac.iie.di.ban.data.exchange.runner.commons.worker.MasterWorker;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author root
 */
public class InterruptHandler implements MasterWorker {

    static Logger logger = Logger.getLogger(InterruptHandler.class);
    private List<String> metadataUrls = null;
//    private MessageProducer<TaskModifyCommand> producer = null;
//    private Consumer<Message<TaskModifyCommand>> consumer;

//    public InterruptHandler(List<String> metadataUrls, Consumer<Message<TaskModifyCommand>> consumer) {
//        this.metadataUrls = metadataUrls;
//        this.consumer = consumer;
//    }
    @Override
    public DataProxyResponse execute(DataProxyRequest workerInfo) {
        return null;
//        String uid = workerInfo.getUid();
//        String req_id = workerInfo.getSql();
//        ScopeEnum scopeEnum = workerInfo.getScope();
//        int scope = scopeEnum.getValue();
//        String uniq_key = workerInfo.getUniq_key();
//        logger.info(req_id + " the uid is " + uid + " ,req_id is " + req_id + " ,scope is " + scope);
//        DataProxyResponse finishResponse = null;
//        String task_id = "";
//        String req_ids = "";
//        String[] req_id_arr = null;
//        TaskModifyResponse stopTaskResponse = null;
//        String detail = "";
//        int running_task_num = 0;
//        String status = "";
//
//        //check
//        HashMap<String, String> requestMap = new HashMap<>();
//        requestMap.put("scope", "request");
//        requestMap.put("opration", "search");
//        Map<String, Object> checkRequestCondition = new HashMap<>();
//        checkRequestCondition.put("req_id", req_id);
//        checkRequestCondition.put("uid", uid);
//        requestMap.put("condition", new JSONObject(checkRequestCondition).toString());
//        requestMap.put("content", "{\"task_id\":\"\"}");
//        String checkRequestReturn = ParserFastJson.requestMetadata(metadataUrls, requestMap);
//        logger.info(req_id + " check req_id and uid, and return " + checkRequestReturn);
//        boolean checkRequestFlag = ParserFastJson.returnError(checkRequestReturn);
//        if (checkRequestFlag) {
//            return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, checkRequestReturn);
//        }
//
//        //得到task_id
//        List<Map<String, String>> requestList = ParserFastJson.getContentByType(checkRequestReturn, "data");
//        if (!requestList.isEmpty()) {
//            Map<String, String> taskMap = requestList.get(0);
//            task_id = taskMap.get("task_id");
//            logger.info(req_id + " search task_id by req_id and uid, and task_id is " + task_id);
//        } else {
//            return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, checkRequestReturn);
//        }
//
//        HashMap<String, String> taskMap = new HashMap<>();
//        taskMap.put("scope", "task");
//        taskMap.put("opration", "search");
//        Map<String, Object> checkTaskCondition = new HashMap<>();
//        checkTaskCondition.put("task_id", task_id);
//        taskMap.put("condition", new JSONObject(checkTaskCondition).toString());
//        taskMap.put("content", "{\"req_id\":\"\"}");
//        String checkTaskReturn = ParserFastJson.requestMetadata(metadataUrls, taskMap);
//        logger.info(req_id + " get req_id from task, and return " + checkTaskReturn);
//        boolean checkTaskFlag = ParserFastJson.returnError(checkTaskReturn);
//        if (checkTaskFlag) {
//            return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, checkTaskReturn);
//        }
//
//        //得到req_ids
//        List<Map<String, String>> taskList = ParserFastJson.getContentByType(checkTaskReturn, "data");
//        if (!taskList.isEmpty()) {
//            Map<String, String> reqMap = taskList.get(0);
//            req_ids = reqMap.get("req_id");
//            logger.info(req_id + " search req_id by task_id, and req_id is " + req_ids);
//        } else {
//            return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, checkRequestReturn);
//        }
//        req_id_arr = req_ids.split(",");
//
//        if (req_id_arr.length == 1) {
//            if (req_id.equals(req_id_arr[0])) {
//                //单个任务,中断任务
//                stopTaskResponse = stopTask(task_id, consumer);
//                if (stopTaskResponse.isSucceeded()) {
//                    logger.info(req_id + " stop single task " + task_id + " successfully");
//                    //修改状态
//                    requestMap.put("opration", "update");
//                    Map<String, Object> updateRequestContent = new HashMap<>();
//                    updateRequestContent.put("status", "6");
//                    requestMap.put("content", new JSONObject(updateRequestContent).toString());
//                    Map<String, Object> updateRequestCondition = new HashMap<>();
//                    updateRequestCondition.put("req_id", req_id);
//                    requestMap.put("condition", new JSONObject(updateRequestCondition).toString());
//                    String updateRerquestReturn = ParserFastJson.requestMetadata(metadataUrls, requestMap);
//                    logger.info(req_id + " update status of request, and return " + updateRerquestReturn);
//                    boolean updateRerquestFlag = ParserFastJson.returnError(updateRerquestReturn);
//                    if (updateRerquestFlag) {
//                        return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, updateRerquestReturn);
//                    }
//
//                    taskMap.put("opration", "update");
//                    Map<String, Object> updateTaskContent = new HashMap<>();
//                    updateTaskContent.put("status", "6");
//                    taskMap.put("content", new JSONObject(updateTaskContent).toString());
//                    Map<String, Object> updateTaskCondition = new HashMap<>();
//                    updateTaskCondition.put("task_id", task_id);
//                    taskMap.put("condition", new JSONObject(updateTaskCondition).toString());
//                    String updateTaskReturn = ParserFastJson.requestMetadata(metadataUrls, taskMap);
//                    logger.info(req_id + " update status of task, and return " + updateTaskReturn);
//                    boolean updateTaskFlag = ParserFastJson.returnError(updateTaskReturn);
//                    if (updateTaskFlag) {
//                        return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, updateTaskReturn);
//                    }
//                } else {
//                    detail = stopTaskResponse.getDetail();
//                    logger.info(req_id + " stop single task " + task_id + " failed, and detail is " + detail);
//                    return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, detail);
//                }
//            } else {
//                String message = req_id + " req_id " + req_id + " of the request and req_id " + req_id_arr[0] + " of the db isn't same.";
//                logger.info(message);
//                return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, message);
//            }
//        } else {
//            //多个任务,判断是否只剩一个正常任务
//            for (String reqid : req_id_arr) {
//                requestMap.put("opration", "search");
//                Map<String, Object> statusRequestCondition = new HashMap<>();
//                statusRequestCondition.put("req_id", reqid);
//                requestMap.put("condition", new JSONObject(statusRequestCondition).toString());
//                requestMap.put("content", "{\"status\":\"\"}");
//                String statusRequestReturn = ParserFastJson.requestMetadata(metadataUrls, requestMap);
//                logger.info(req_id + " get status from request, and return " + statusRequestReturn);
//                boolean statusRequestFlag = ParserFastJson.returnError(statusRequestReturn);
//                if (statusRequestFlag) {
//                    return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, statusRequestReturn);
//                }
//                List<Map<String, String>> statusList = ParserFastJson.getContentByType(statusRequestReturn, "data");
//                if (!statusList.isEmpty()) {
//                    Map<String, String> statusMap = statusList.get(0);
//                    status = statusMap.get("status");
//                    logger.debug(req_id + " this req_id " + reqid + " status is " + status);
//                } else {
//                    return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, statusRequestReturn);
//                }
//                if ("3".equals(status) || "2".equals(status) || "1".equals(status)) {
//                    running_task_num++;
//                }
//            }
//            logger.info(req_id + " running_task_num is " + running_task_num);
//            if (running_task_num == 1) {
//                stopTaskResponse = stopTask(task_id, consumer);
//                if (stopTaskResponse.isSucceeded()) {
//                    logger.info(req_id + " stop the last task " + task_id + " successfully");
//                    //修改状态
//                    requestMap.put("opration", "update");
//                    Map<String, Object> updateRequestContent = new HashMap<>();
//                    updateRequestContent.put("status", "6");
//                    requestMap.put("content", new JSONObject(updateRequestContent).toString());
//                    Map<String, Object> updateRequestCondition = new HashMap<>();
//                    updateRequestCondition.put("req_id", req_id);
//                    requestMap.put("condition", new JSONObject(updateRequestCondition).toString());
//                    String updateRerquestReturn = ParserFastJson.requestMetadata(metadataUrls, requestMap);
//                    logger.info(req_id + " update status of request, and return " + updateRerquestReturn);
//                    boolean updateRerquestFlag = ParserFastJson.returnError(updateRerquestReturn);
//                    if (updateRerquestFlag) {
//                        return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, updateRerquestReturn);
//                    }
//
//                    taskMap.put("opration", "update");
//                    Map<String, Object> updateTaskContent = new HashMap<>();
//                    updateTaskContent.put("status", "6");
//                    taskMap.put("content", new JSONObject(updateTaskContent).toString());
//                    Map<String, Object> updateTaskCondition = new HashMap<>();
//                    updateTaskCondition.put("task_id", task_id);
//                    taskMap.put("condition", new JSONObject(updateTaskCondition).toString());
//                    String updateTaskReturn = ParserFastJson.requestMetadata(metadataUrls, taskMap);
//                    logger.info(req_id + " update status of task, and return " + updateTaskReturn);
//                    boolean updateTaskFlag = ParserFastJson.returnError(updateTaskReturn);
//                    if (updateTaskFlag) {
//                        return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, updateTaskReturn);
//                    }
//                } else {
//                    detail = stopTaskResponse.getDetail();
//                    logger.info(req_id + " stop single task " + task_id + " failed, and detail is " + detail);
//                    return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, detail);
//                }
//            } else {
//                logger.info(req_id + " has more than one normal task, just update status of request");
//                requestMap.put("opration", "update");
//                Map<String, Object> updateRequestContent = new HashMap<>();
//                updateRequestContent.put("status", "6");
//                requestMap.put("content", new JSONObject(updateRequestContent).toString());
//                Map<String, Object> updateRequestCondition = new HashMap<>();
//                updateRequestCondition.put("req_id", req_id);
//                requestMap.put("condition", new JSONObject(updateRequestCondition).toString());
//                String updateRerquestReturn = ParserFastJson.requestMetadata(metadataUrls, requestMap);
//                logger.info(req_id + " update status of request, and return " + updateRerquestReturn);
//                boolean updateRerquestFlag = ParserFastJson.returnError(updateRerquestReturn);
//                if (updateRerquestFlag) {
//                    return new DataProxyResponse(uniq_key, req_id, StatusEnum.CHECK_SQL_ERROR, updateRerquestReturn);
//                }
//            }
//        }
//        finishResponse = new DataProxyResponse(uniq_key, req_id, StatusEnum.INTERRUPTED, "success");
//        logger.info(req_id + " response master end");
//        return finishResponse;
    }

//    private TaskModifyResponse stopTask(String task_id, Consumer<Message<TaskModifyCommand>> consumer) {
//        TaskModifyMessage tmMessage = new TaskModifyMessage(TaskModifyCommand.CmdTypeEnum.Delete, null, new TaskInfo(task_id));
//        consumer.accept(tmMessage);
//        return tmMessage.getResponse();
//    }
    @Override
    public String getName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public DataProxyResponse redo(TaskInfo ti) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean finished(TaskFinishOrderRequest tfor) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void stop() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
